package www.dianbaozu.com.ui.server

import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.netease.nim.uikit.api.NIMHelper
import com.netease.nimlib.sdk.team.constant.TeamMessageNotifyTypeEnum
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentNotificationSettingsBinding
import www.dianbaozu.com.extension.requestUniversally
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.data.entities.*
import www.wen.com.common.lib.extension.postObject
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.KeyHelper.BUNDLE_DATA
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter


/**
 * @author WEN
 * @Description:通知设置
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class NotificationSettingsFragment : BaseLoadingFragment<FragmentNotificationSettingsBinding>(),
        OnItemClickListener {

    private val mMessageTypeList = mutableListOf(
            MessageTypeChooseEntity(MessageType.ALL_MESSAGES),
            MessageTypeChooseEntity(MessageType.APPOINT_MESSAGES),
            MessageTypeChooseEntity(MessageType.NO_NOTICE))

    private var mCurrentPosition = -1

    private val mBaseApiViewModel: BaseApiViewModel by viewModels()

    private lateinit var mBundleNotificationEntity: BundleNotificationEntity

    override val viewModel: BaseApiViewModel
        get() = mBaseApiViewModel

    override fun isEnableLoadingHelper() = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(
            requireActivity(),
            tilte = getString(R.string.notification_settings),
            action = ::titleAction
    )

    override fun getLayoutId(): Int = R.layout.fragment_notification_settings

    override fun lazyInitView() {
        mBundleNotificationEntity = requireArguments().getParcelable(BUNDLE_DATA)!!
        mCurrentPosition = mMessageTypeList.indexOfFirst { mBundleNotificationEntity.noticeType == it.msg.remoteType }
        mMessageTypeList[mCurrentPosition].isSelected = true
        dataBinding.rvMessageTypes.run {
            layoutManager = LinearLayoutManager(requireActivity())
            adapter = NotificationSettingsAdapter(mMessageTypeList).apply {
                setOnItemClickListener(this@NotificationSettingsFragment)
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        if (position != mCurrentPosition) {
            val message = adapter.data[position] as MessageTypeChooseEntity
            mBaseApiViewModel.requestUniversally(
                    viewModelScope = lifecycleScope,
                    isShowToastTip = false,
                    request = { appDataManage ->
                        appDataManage.requestMemberSetting(
                                id = mBundleNotificationEntity.Id,
                                RequestBodyParameter(
                                        noticeType = message.msg.remoteType,
                                        serverId = mBundleNotificationEntity.serverId
                                )
                        )
                    }
            ) {
                val teamMessageNotifyTypeEnum = when (message.msg.remoteType) {
                    "0" -> TeamMessageNotifyTypeEnum.Mute
                    "1" -> TeamMessageNotifyTypeEnum.All
                    //没有只显示@消息
                    "2" -> TeamMessageNotifyTypeEnum.Mute
                    else -> TeamMessageNotifyTypeEnum.All
                }
                mBundleNotificationEntity.teamIds?.forEach {
                    NIMHelper.muteTeam(it, teamMessageNotifyTypeEnum)
                }
                val clickPosition = adapter.data[position] as MessageTypeChooseEntity
                val currentPosition = adapter.data[mCurrentPosition] as MessageTypeChooseEntity
                clickPosition.isSelected = true
                currentPosition.isSelected = false
                adapter.notifyItemChanged(mCurrentPosition)
                adapter.notifyItemChanged(position)
                mCurrentPosition = position
                postObject(KeyHelper.REFRESH_SERVER_MESSAGE_TYPE_KEY, clickPosition.msg.remoteType)
                actionLeft()
            }
        }
    }

    internal class NotificationSettingsAdapter(groups: MutableList<MessageTypeChooseEntity>) : BaseQuickAdapter<MessageTypeChooseEntity, BaseViewHolder>(R.layout.item_grouping_choose, groups) {

        override fun convert(holder: BaseViewHolder, item: MessageTypeChooseEntity) {
            holder.setVisible(R.id.imageView4, item.isSelected)
            holder.setText(R.id.tvCreateGroup, item.msg.messageStr)
        }
    }

}



